<template>
  <div>
    <div class="title2 title2-padding">发送长指令</div>
    <el-form  :model="form" label-width="100px">
      <el-form-item label="">
        <ReadFile ref="readfileRef" @read="handleReadFile"></ReadFile>
      </el-form-item>
      <el-form-item label="指令" v-show="form.fileContent !== ''">
        <el-input v-model="form.fileContent" type="textarea" :rows="8" disabled></el-input>
      </el-form-item>
      <el-form-item label="间隔时间">
        <el-input v-model="form.intervalTime" type="number" placeholder="请输入间隔时间">
          <template #append>毫秒</template>
        </el-input>
      </el-form-item>
      <el-form-item v-show="form.fileContent !== ''">
        <el-button type="primary" @click="submit" :loading="loading">发送</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import ReadFile from '@/components/ReadFile.vue';
import { setTcCmd } from '@/api/loads-api';
import { failedNotify, successNotify } from "@/comm/index.js";
import { useTcStore } from '@/stores/tc-module.js'

const tcStore = useTcStore()
const readfileRef = ref(null)
const form = ref({
  intervalTime: 3000,
  fileContent: ''
})
let timerId = 0
let firstFg = true
let loading = ref(false);

const handleReadFile = (content) => {
  form.value.fileContent = content
}

const submit = () => {
  loading.value = true
  let cmdArr = form.value.fileContent.split(/\r?\n/)
  cmdArr = cmdArr.filter(item => item.trim() !== '')
  let intCmdArr = []
  for( let i = 0; i < cmdArr.length; i++) {
    const lineArr = cmdArr[i].split(' ')
    intCmdArr[i] = []
    for (let j = 0; j < lineArr.length; j++) {
      if (lineArr[j] !== '') {
        intCmdArr[i].push(parseInt(lineArr[j], 16))
      }
    }
  }
  console.log("intCmdArr:",intCmdArr)

  loopThoughArray(intCmdArr, 0, (index,item) => {
    let reqJson = { id: -1, cmdName: '长指令', valueList: item }
    sendApi(reqJson, index === cmdArr.length - 1)
  })
}

const loopThoughArray = (arr, index, callback) => {
  const len = arr.length
  if (index < len ) {
    callback(index,arr[index])
    timerId = setTimeout(() => loopThoughArray(arr,index+1, callback), form.value.intervalTime)
  }
}

const sendApi = (reqJson, lastFg = false) => {
  setTcCmd(reqJson, (response => {
    successNotify('参数设置成功!')
    afterSendTc(lastFg, reqJson, response.message)
  }), (error => {
    afterSendTc(lastFg, reqJson, error.message || errror.toString())
    failedNotify('参数设置失败!', error)
    // clearTimeout(timerId)
  }))
}

const afterSendTc = (lastFg = false, reqJson,  response) => {
  firstFg = false
  if(lastFg) {
    loading.value = false
    firstFg = true
  }
  const logObj = {...reqJson, ...{time: new Date().toLocaleString()}, ...{ result: response }}
  tcStore.setLongTcLog(logObj)
}

const cancel = () => {
  clearTimeout(timerId)
  loading.value = false
  firstFg = true
  // readfileRef.value.clearFile()
}
</script>